<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_feieyun/model')->方法名();
*/
namespace jy_feieyun;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function cloud_list($setting=array()){
		$setting=$setting?:$this->plugin_setting('setting');
		if(!$setting || !$setting['user'] || !$setting['ukey']){
			return error('接口参数错误！');
		}
		$result=$this->model('jinyunzn',$setting)->get_list();
		if(!is_array($result) || $result['code']>0){
			return error('获取用户打印机失败！'.$result['message']);
		}else{
			return $result['message'];
		}
	}
	public function toprint($id=0,$content='',$nowait=false,$print_nums=1,$setting=array(),$isexi=false){
		$setting=$setting?:$this->plugin_setting('setting');
		if(!$setting || !$setting['user'] || !$setting['ukey']){
			if($isexi){
				exi('接口参数错误！联系管理员设置！','error');
			}
			return error('接口参数错误！联系管理员设置！');
		}
		$print=pdo_get('jy_feieyun_print',array('id'=>$id));
		$seri_num=$print['sn'];
		if(!$seri_num){
			if($isexi){
				exi('指定打印机不存在','error');
			}
			return error('指定打印机不存在！');
		}
		if($print_nums>1 && $print_nums<=10){
			for($i=0;$i<$print_nums;$i++){
				$nums = $i+1;
				$times_str = "《第".$nums."联》\n";
				$times_str .= "-----------------------\n";
				$pscontent = $times_str . $content;
				$result=$this->model('jinyunzn',$setting)->printing($seri_num,$pscontent,$print['type'],$nowait);
			}
		}else{
			$result=$this->model('jinyunzn',$setting)->printing($seri_num,$content,$print['type'],$nowait);
		}
		if(is_array($result) && $result['ret']==0){
			return true;
		}else{
			if($isexi){
				exi($result['msg'],'error');
			}
			return error($result['msg']);
		}
	}
	/**
	 * [统计字符串字节数补空格，实现左右排版对齐]
	 * @param  [string] $str_left    [左边字符串]
	 * @param  [string] $str_right   [右边字符串]
	 * @param  [int]    $length      [输入当前纸张规格一行所支持的最大字母数量]
	 *                               58mm的机器,一行打印16个汉字,32个字母;76mm的机器,一行打印22个汉字,33个字母,80mm的机器,一行打印24个汉字,48个字母
	 *                               标签机宽度50mm，一行32个字母，宽度40mm，一行26个字母
	 * @return [string]              [返回处理结果字符串]
	 */
	public function LR($str_left,$str_right,$length){
		if( empty($str_left) || empty($str_right) || empty($length) ) return '请输入正确的参数';
		$kw = '';
		$str_left_lenght = strlen(iconv("UTF-8", "GBK//IGNORE", $str_left));
		$str_right_lenght = strlen(iconv("UTF-8", "GBK//IGNORE", $str_right));
		$k = $length - ($str_left_lenght+$str_right_lenght);
		for($q=0;$q<$k;$q++){
			$kw .= ' ';
		}
		return $str_left.$kw.$str_right;
	}
}
?>